package com.mxy.batterychargingreminding;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v4.app.NotificationCompat;

/**
 * 电池变更广播接收器
 *
 * @author 马晓勇
 */

public class BatteryChangeReceiver extends BroadcastReceiver {

    private static final int NOTICE_ID = 121446;
    private static final String NOTIFICATION_CHANNEL_ID = "2";
    private static final String NOTIFICATION_CHANNEL_NAME = "highImportanceChannel";

    @Override
    public void onReceive(Context context, Intent intent) {
        //电池状态变更广播
        if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
            Util.print("ACTION_BATTERY_CHANGED");
            Util.print(String.valueOf("当前电量百分比: " + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)));
            switch (intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1)) {
                case BatteryManager.BATTERY_HEALTH_COLD:
                    Util.print("电池健康状况:温度过低");
                    break;
                case BatteryManager.BATTERY_HEALTH_DEAD:
                    Util.print("电池健康状况:没电");
                    break;
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    Util.print("电池健康状况:良好");
                    break;
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    Util.print("电池健康状况:电压过高");
                    break;
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    Util.print("电池健康状况:过热");
                    break;
                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                    Util.print("电池健康状况:电池状态获取失败");
                    break;
                case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                    Util.print("电池健康状况:未知错误");
                    break;
                default:
                    break;
            }
            switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    Util.print("手机充电中");
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    Util.print("手机放电中");
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    Util.print("电池已满");
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    Util.print("手机未充电");
                    break;
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    Util.print("手机状态未知");
                    break;
                default:
                    break;
            }
            switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
                case BatteryManager.BATTERY_PLUGGED_AC:
                    Util.print("充电器充电");
                    break;
                case BatteryManager.BATTERY_PLUGGED_USB:
                    Util.print("USB充电");
                    break;
                case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                    Util.print("无线充电");
                    break;
                default:
                    break;
            }
            Util.print("电池温度: " + (float) (intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10) + "度");

            //电量100并且充电中
            if (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) == BatteryManager.BATTERY_STATUS_FULL && intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0) {

                NotificationCompat.Builder builder = Util.getNotificationBuilder(context, NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, true);
                if (builder != null) {
                    builder.setSmallIcon(R.drawable.small_icon);
                    builder.setTicker(context.getResources().getString(R.string.fullbattery));
                    builder.setContentTitle(context.getString(R.string.app_name));
                    builder.setContentText(context.getResources().getString(R.string.fullbattery));
                    //发送时间
                    builder.setWhen(System.currentTimeMillis());
                    builder.setDefaults(NotificationCompat.DEFAULT_ALL);
                    builder.setPriority(NotificationCompat.PRIORITY_MAX);
                    builder.setAutoCancel(true);
                    Notification notification = builder.build();

                    NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                    if (notificationManager != null) {
                        notificationManager.notify(NOTICE_ID, notification);
                    }
                }
            }
        }
        //手机亏电广播
        if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {
            Util.print("ACTION_BATTERY_LOW");
            Util.print(String.valueOf("当前电量百分比: " + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)));
            switch (intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1)) {
                case BatteryManager.BATTERY_HEALTH_COLD:
                    Util.print("电池健康状况:温度过低");
                    break;
                case BatteryManager.BATTERY_HEALTH_DEAD:
                    Util.print("电池健康状况:没电");
                    break;
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    Util.print("电池健康状况:良好");
                    break;
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    Util.print("电池健康状况:电压过高");
                    break;
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    Util.print("电池健康状况:过热");
                    break;
                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                    Util.print("电池健康状况:电池状态获取失败");
                    break;
                case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                    Util.print("电池健康状况:未知错误");
                    break;
                default:
                    break;
            }
            switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    Util.print("手机充电中");
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    Util.print("手机放电中");
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    Util.print("电池已满");
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    Util.print("手机未充电");
                    break;
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    Util.print("手机状态未知");
                    break;
                default:
                    break;
            }
            switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
                case BatteryManager.BATTERY_PLUGGED_AC:
                    Util.print("充电器充电");
                    break;
                case BatteryManager.BATTERY_PLUGGED_USB:
                    Util.print("USB充电");
                    break;
                case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                    Util.print("无线充电");
                    break;
                default:
                    break;
            }
            Util.print("电池温度: " + (float) (intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10) + "度");

            //手机亏电并且手机未充电
            if (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) == 0) {

                NotificationCompat.Builder builder = Util.getNotificationBuilder(context, NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, true);
                if (builder != null) {
                    builder.setSmallIcon(R.drawable.small_icon);
                    builder.setTicker(context.getResources().getString(R.string.lowbattery));
                    builder.setContentTitle(context.getString(R.string.app_name));
                    builder.setContentText(context.getResources().getString(R.string.lowbattery));
                    //发送时间
                    builder.setWhen(System.currentTimeMillis());
                    builder.setDefaults(NotificationCompat.DEFAULT_ALL);
                    builder.setPriority(NotificationCompat.PRIORITY_MAX);
                    builder.setAutoCancel(true);
                    Notification notification = builder.build();

                    NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                    if (notificationManager != null) {
                        notificationManager.notify(NOTICE_ID, notification);
                    }
                }
            }
        }
        if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
            Util.print("ACTION_POWER_CONNECTED");
            NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.cancelAll();
            }
        }
        if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
            Util.print("ACTION_POWER_DISCONNECTED");
            NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.cancelAll();
            }
        }
    }
}